
// 1. Pre-processor Directives Section
// Constant declarations to access port registers using 
// symbolic names instead of addresses
#include "TExaS.h"
#include "tm4c123gh6pm.h"

// 2. Declarations Section
//   Global Variables
unsigned long In;  // input from PF4
unsigned long Out; // outputs to PF3,PF2,PF1 (multicolor LED)

//   Function Prototypes
void PortF_Init(void);
void PortE_Init(void);
void Delay(void);
void Delay100ms();
void EnableInterrupts(void);


// 3. Subroutines Section

int main(void){    
	TExaS_Init(SW_PIN_PF40,LED_PIN_PF321); // this initializes the TExaS grader lab 2
  PortF_Init();        // Call initialization of port PF4 PF2   
  PortE_Init();	
  EnableInterrupts();  // The grader uses interrupts
  while(1){
		
		GPIO_PORTE_DATA_R = 0x00;
		
		GPIO_PORTF_DATA_R = 0x06;
		Delay100ms(20);
		
		GPIO_PORTF_DATA_R = 0x03;		
    Delay100ms(5);           
		
    GPIO_PORTF_DATA_R = 0x18;    
    Delay100ms(20);                     
		
		GPIO_PORTF_DATA_R = 0x10;
		
    GPIO_PORTE_DATA_R = 0x02;   
    Delay100ms(5); 			
		
  }
}

void PortE_Init(void){ volatile unsigned long delay;
  SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOE;     // 1) F clock
  delay = SYSCTL_RCGC2_R;           // delay   
       
  GPIO_PORTE_AMSEL_R &= ~0x03;        // 3) disable analog function
  GPIO_PORTE_PCTL_R &= ~0x000000FF;   // 4) GPIO clear bit PCTL  
  GPIO_PORTE_DIR_R &= ~0x01;         // 5.1) PE0 input, 
  GPIO_PORTE_DIR_R |= 0x02;          // 5.2) PE1 output  
  GPIO_PORTE_AFSEL_R &= 0x00;        // 6) no alternate function
  GPIO_PORTE_DEN_R |= 0x1F;          // 7) enable digital pins PE4-PE0     
}

void PortF_Init(void){ volatile unsigned long delay;
  SYSCTL_RCGC2_R |= 0x00000020;     // 1) F clock
  delay = SYSCTL_RCGC2_R;           // delay   
  GPIO_PORTF_LOCK_R = 0x4C4F434B;   // 2) unlock PortF PF0  
  GPIO_PORTF_CR_R = 0x1F;           // allow changes to PF4-0       
  GPIO_PORTF_AMSEL_R = 0x00;        // 3) disable analog function
  GPIO_PORTF_PCTL_R = 0x00000000;   // 4) GPIO clear bit PCTL  
  GPIO_PORTF_DIR_R = 0x1F;          // 5) PF4,PF3,PF2,PF1,PF0 output   
  GPIO_PORTF_AFSEL_R = 0x00;        // 6) no alternate function
  GPIO_PORTF_PUR_R = 0x00;          // enable pullup resistors on PF4,PF0       
  GPIO_PORTF_DEN_R = 0x1F;          // 7) enable digital pins PF4-PF0        
}

void Delay(void){unsigned long volatile time;
  time = 727240*200/91;  // 0.1sec
  while(time){
		time--;
  }
}

	void Delay100ms(unsigned long time){
  unsigned long i;
  while(time > 0){
    i = 1333333;  // this number means 100ms
		
    while(i > 0){
      i = i - 1;
    }
    time = time - 1; // decrements every 100 ms
  }
}
